<template>
	<view class="container">
		<u-tabs :list="tabs" :current="current" @change="tabsChange"></u-tabs>
		
		<!-- 已发布岗位列表 -->
		<view v-if="current === 0">
			<view class="vacancy-list">
				<u-card v-for="(item, index) in publishedList" :key="index" :title="item.vacancyName" :sub-title="item.createTime">
					<view slot="body">
						<view class="vacancy-info">
							<text class="salary">{{item.salary}}</text>
							<text class="address">{{item.address}}</text>
						</view>
						<view class="vacancy-stats">
							<text>申请人数：{{item.applyCount || 0}}</text>
						</view>
					</view>
					<view slot="foot" class="card-foot">
						<u-button type="primary" size="mini" @click="editVacancy(item)">编辑</u-button>
						<u-button type="error" size="mini" @click="deleteVacancy(item)">删除</u-button>
					</view>
				</u-card>
			</view>
			<u-loadmore :status="loadStatus" />
		</view>
		
		<!-- 收到的申请列表 -->
		<view v-if="current === 1">
			<view class="apply-list">
				<u-card v-for="(item, index) in applyList" :key="index" :title="item.vacancyName" :sub-title="item.createTime">
					<view slot="body">
						<view class="apply-info">
							<text class="applicant">申请人：{{item.userName}}</text>
							<text class="status" :class="getStatusClass(item.status)">{{getStatusText(item.status)}}</text>
						</view>
						<view class="apply-detail">
							<text>联系电话：{{item.userPhone}}</text>
						</view>
					</view>
					<view slot="foot" class="card-foot" v-if="item.status === 0">
						<u-button type="success" size="mini" @click="reviewApply(item, 1)">通过</u-button>
						<u-button type="error" size="mini" @click="reviewApply(item, 2)">拒绝</u-button>
					</view>
				</u-card>
			</view>
			<u-loadmore :status="loadStatus" />
		</view>
	</view>
</template>

<script>
import { pageSearch, applyList, reviewApply } from '../../utils/api/modules/vacancy';

export default {
	data() {
		return {
			tabs: [{
				name: '已发布岗位'
			}, {
				name: '收到的申请'
			}],
			current: 0,
			publishedList: [],
			applyList: [],
			pageNum: 1,
			pageSize: 10,
			loadStatus: 'loading'
		}
	},
	onLoad() {
		this.loadData();
	},
	methods: {
		tabsChange(index) {
			this.current = index;
			this.pageNum = 1;
			this.loadData();
		},
		async loadData() {
			const user = uni.getStorageSync('user');
			if (!user || !user.id) {
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				});
				return;
			}
			
			try {
				if (this.current === 0) {
					const res = await pageSearch({
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						posterId: user.id
					});
					this.publishedList = res.data.records;
				} else {
					const res = await applyList({
						pageNum: this.pageNum,
						pageSize: this.pageSize,
						companyId: user.id
					});
					this.applyList = res.data.records;
				}
				this.loadStatus = this.pageNum * this.pageSize >= res.data.total ? 'nomore' : 'loadmore';
			} catch (error) {
				console.error('加载数据失败:', error);
				uni.showToast({
					title: '加载失败',
					icon: 'none'
				});
			}
		},
		editVacancy(item) {
			uni.navigateTo({
				url: `/pages/enterprise/vacancy-edit?id=${item.id}`
			});
		},
		deleteVacancy(item) {
			uni.showModal({
				title: '提示',
				content: '确定要删除该岗位吗？',
				success: async (res) => {
					if (res.confirm) {
						try {
							await deleteVacancy({ id: item.id });
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							});
							this.loadData();
						} catch (error) {
							uni.showToast({
								title: '删除失败',
								icon: 'none'
							});
						}
					}
				}
			});
		},
		async reviewApply(item, status) {
			try {
				await reviewApply({
					id: item.id,
					status: status
				});
				uni.showToast({
					title: status === 1 ? '已通过' : '已拒绝',
					icon: 'success'
				});
				this.loadData();
			} catch (error) {
				uni.showToast({
					title: '操作失败',
					icon: 'none'
				});
			}
		},
		getStatusText(status) {
			const statusMap = {
				0: '待审核',
				1: '已通过',
				2: '已拒绝'
			};
			return statusMap[status] || '未知状态';
		},
		getStatusClass(status) {
			const classMap = {
				0: 'status-pending',
				1: 'status-approved',
				2: 'status-rejected'
			};
			return classMap[status] || '';
		}
	}
}
</script>

<style>
.container {
	padding: 20rpx;
}

.vacancy-list, .apply-list {
	margin-top: 20rpx;
}

.vacancy-info, .apply-info {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 10rpx;
}

.salary {
	color: #ff5500;
	font-weight: bold;
}

.address {
	color: #666;
	font-size: 28rpx;
}

.vacancy-stats, .apply-detail {
	color: #666;
	font-size: 28rpx;
}

.card-foot {
	display: flex;
	justify-content: flex-end;
	gap: 20rpx;
}

.status {
	font-size: 28rpx;
}

.status-pending {
	color: #ff9900;
}

.status-approved {
	color: #19be6b;
}

.status-rejected {
	color: #fa3534;
}

.applicant {
	font-weight: bold;
}
</style> 